<?php

class BlueprintComponent {
	
	
	function BlueprintComponent(&$element, $argsString) {
		$args = $this->parseArgs($argsString);
		array_unshift($args, null);
		$args[0] =& $element;
		call_user_func_array(array($this, 'parse'), $args);
	}
	
	function parse(&$element) {
		// override this method (would be abstract except that the number of parameters is variable
	}
	
	function parseArgs($string) {
		$args = array();
		$encloseChars = array("'" => "'", '"' => '"', '{' => '}', '[' => ']', '(' => ')');
		$sb = '';
		$ch = '';
		$enclosures = array();
		$str_length = strlen($string);
		for ($i = 0; $i < $str_length; $i++) {
			$ch = $string{$i};
			if ($ch == $enclosures[sizeof($enclosures) - 1]) {
				array_pop($enclosures);
			} elseif (isset($encloseChars[$ch])) {
				$enclosures[] = $encloseChars[$ch];
			} elseif (sizeof($enclosures) == 0 && $ch == ',') {
				$args[] = $this->parseVars(trim($sb));
				$sb = '';
				continue;
			}
			$sb .= $ch;
		}
		$arg = $this->parseVars(trim($sb));
		if (strtolower($arg) == 'true') {
			$arg = true;
		} elseif (strtolower($arg) == 'false') {
			$arg = false;
		} elseif (is_numeric($arg)) {
			$arg = floatval($arg);
		}
		$args[] = $arg;
		return $args;
	}
	
	function parseVars($string) {
		return preg_replace_callback('/{([^}]+)}/', create_function('$matches', 'return getPathChain($matches[1]);'), $string);
	}
	
	function parseCodeInUrl($string) {
		return preg_replace_callback('/{([^}]+)}/', create_function('$matches', 'return "<?php echo {$matches[1]}; ?>";'), $string);
	}
}

?>